﻿using Instagram.Version2.Helper;
using Instagram.Version2.Models;
using System;
using Windows.Devices.Geolocation;
using Windows.Media.Capture;
using Windows.Media.MediaProperties;
using Windows.Services.Maps;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

// The Content Dialog item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace Instagram.Version2
{
    public sealed partial class ConfirmFileSaving : ContentDialog
    {
        private IStorageFile _photoFile;
        private MediaCapture _captureManager;
        private UserModel _user;

        public ConfirmFileSaving(MediaCapture captureManager, IStorageFile photoFile, ImageSource imageSource, UserModel user, string address)
        {
            this.InitializeComponent();

            imagePreview.Source = imageSource;
            _captureManager = captureManager;
            _photoFile = photoFile;
            _user = user;
            locationTxt.Text = address;
        }

        async private void Save(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            // create a file
            _photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync(name.Text + ".jpg", CreationCollisionOption.ReplaceExisting);

            // take a photo with choosen Encoding
            await _captureManager.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), _photoFile);

            var isUploaded = await ApiCaller.PostAsync("api/Photos/PostPhoto", new PhotoModel
            {
                Title = name.Text,
                Created = DateTime.Now,
                User = _user.Name,
                Image = await Converter.GetBytes(_photoFile),
                Location = locationTxt.Text
            });
        }

        private void Cancel(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
        }
    }
}
